﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using groupAnalyzer.model;
using System.ComponentModel;
using groupAnalyzer.Observers;


namespace groupAnalyzer
{
    public class ViewFactory : INotifyPropertyChanged
    {

        private model.Factory realFactory;

        private volatile Collection<NodeView> userNodes;
        private volatile Collection<GroupView> groups;
        
        private volatile Collection<BaseView> ldapElements;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public void setFactory(model.Factory factory)
        {
            this.realFactory = factory;
        }

        public Collection<NodeView> Nodes
        {
            get
            {
                return this.getNodes();
            }
        }

        public Collection<GroupView> Groups
        {
            get
            {
                return this.getGroups();
            }
        }

        public Collection<BaseView> Tree
        {
            get
            {
                return this.getLdapElements();
            }
        }

        public Collection<BaseView> getLdapElements()
        {
            this.copyLdapElements();
            return this.ldapElements;
        }

        public void copyGroups()
        {
            lock (this.userNodes)
            {

                Collection<GroupView> nodeTmp = new Collection<GroupView>();
                List<Group> nodes = this.realFactory.getGroups();
                foreach (Group group in nodes)
                {
                    nodeTmp.Add(new GroupView(group));
                }
                this.groups = nodeTmp;
            }
        }

        public Collection<GroupView> getGroups()
        {
            this.copyGroups();
            return this.groups;
        }

        public void copyNodes()
        {
            lock (this.userNodes)
            {

                Collection<NodeView> nodeTmp = new Collection<NodeView>();
                List<Node> nodes = this.realFactory.getRootNodes();
                foreach (Node node in nodes)
                {
                    nodeTmp.Add(new NodeView(node,NodeConnectionMode.normal));
                }
                this.userNodes = nodeTmp;
            }
        }

        public Collection<NodeView> getNodes()
        {
            this.copyNodes();
            return this.userNodes;
        }


        public void copyLdapElements()
        {
            this.ldapElements.Add(new ContainerView(new groupAnalyzer.model.Container("abc", "abc")));
        }



        public ViewFactory(model.Factory factory)
        {
            this.realFactory = factory;

            this.userNodes = new Collection<NodeView>();
            this.groups = new Collection<GroupView>();
            this.ldapElements = new Collection<BaseView>();
        }

        private GroupView createGroupView(Group group)
        {
            return (new GroupView(group));
        }

        private UserView createUserView(User user)
        {
            return (new UserView(user));
        }

        private ContainerView createContainerView(groupAnalyzer.model.Container container) {
            return (new ContainerView(container));
        }

        private OUView createOUView(groupAnalyzer.model.OU ou)
        {
            return new OUView(ou);
        }
    }
}
